package com.bjtu.lms.action.admin;

import com.bjtu.lms.model.Administrator;
import com.bjtu.lms.service.AdminService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;

/**
 *
 * @author yilee
 */
public class AdminLoginAction extends ActionSupport {

	private AdminService adminService;
	private String username;
	private String password;
	private String result;

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public String execute() throws Exception {
		Map session = ActionContext.getContext().getSession();
		if (username == null || password == null || username.equals("") || password.equals("")) {
			result = "{suc:0, msg:'请输入您的用户名、密码...'}";
			session.put("user", null);
		} else {
			Administrator a = adminService.login(username, password);
			if (a != null) {
				result = "{suc:1, msg:'登陆成功...'}";
				session.put("admin", a);
			} else {
				result = "{suc:0, msg:'登陆失败...'}";
				session.put("admin", null);
			}
		}
		return SUCCESS;
	}
}
